package net.swl.linkedvillage.database;

import java.util.ArrayList;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;


public class PieceConnector {
	
	private Context contexto;
	private DBAdapter dbadapter;
	private String[] camposDB = {"_id", "player_id", "type",
			"height", "width", "corR", "corG", "corB", "x",
			"y", "radiusX","radiusY", "alpha"};
	
	public PieceConnector(Context contexto) {
		this.contexto = contexto;
		dbadapter = DBAdapter.getInstance(contexto);
	}
	
	/*
	public ArrayList<Piece> loadPieces(String player_id){
		
		ArrayList<Piece> pieces = new ArrayList<Piece>();
		Piece p;
		
		Cursor cur = dbadapter.getmDb().query("piece", camposDB, "player_id=?", new String[]{player_id}, null, null, null);
		
		while (cur.moveToNext()) {
			p= parseSinglePiece(cur);
			pieces.add(p);
		}
		
		pieces.trimToSize();
		
		if (pieces.size() == 0)
			pieces = null;
		cur.close();
		
		return pieces;
	}
	
	
	private Piece parseSinglePiece(Cursor cur){
		Piece p = new Piece();
		p.setId(cur.getInt(cur.getColumnIndex("_id")));
		p.setPlayer_id(cur.getString(cur.getColumnIndex("player_id")));
		p.setType(cur.getString(cur.getColumnIndex("type")));
		p.setHeight(Float.parseFloat(cur.getString(cur.getColumnIndex("height"))));
		p.setWidth(Float.parseFloat(cur.getString(cur.getColumnIndex("width"))));
		p.setCorR(Float.parseFloat(cur.getString(cur.getColumnIndex("corR"))));
		p.setCorG(Float.parseFloat(cur.getString(cur.getColumnIndex("corG"))));
		p.setCorB(Float.parseFloat(cur.getString(cur.getColumnIndex("corB"))));
		p.setX(Float.parseFloat(cur.getString(cur.getColumnIndex("x"))));
		p.setY(Float.parseFloat(cur.getString(cur.getColumnIndex("y"))));
		p.setRadiusX(Float.parseFloat(cur.getString(cur.getColumnIndex("radiusX"))));
		p.setRadiusY(Float.parseFloat(cur.getString(cur.getColumnIndex("radiusY"))));
		p.setAlpha(Float.parseFloat(cur.getString(cur.getColumnIndex("alpha"))));
		return p;
	}
	

	public boolean save(ArrayList<Piece> pieces){
		
		ContentValues vals;
		
		try{
		for (Piece p : pieces){
			if(p==null)
				continue;
			
			vals = new ContentValues();
			vals.put("player_id", p.getPlayer_id());
			vals.put("type", p.getType());
			vals.put("height", Float.toString(p.getHeight()));
			vals.put("width", Float.toString(p.getWidth()));
			vals.put("corR", Float.toString(p.getCorR()));
			vals.put("corG", Float.toString(p.getCorG()));
			vals.put("corB", Float.toString(p.getCorB()));
			vals.put("x", Float.toString(p.getX()));
			vals.put("y", Float.toString(p.getY()));
			vals.put("radiusX", Float.toString(p.getRadiusX()));
			vals.put("radiusY", Float.toString(p.getRadiusY()));
			vals.put("alpha", Float.toString(p.getAlpha()));
			
			long rowId = dbadapter.getmDb().insert("piece", null, vals);
		}
		
		}catch (Exception e) {
			Log.e("erro savePieces", e.toString());
			return false;
		}

		return true;
	}
	*/
}
